﻿using System;
using System.Collections.Generic;
using System.Text;
using CMN.BLL;
using CMN.Entity;
using CMN.Cache;
using CMN.Common;
using System.Web;
namespace CMN.Framework
{
    public  class PermissionManger
    {
        /// <summary>
        /// 获取单个权限列表
        /// </summary>
        /// <param name="permissionId"></param>
        /// <returns></returns>
        public static Permission GetPermission(int permissionId)
        {

            List<Permission> permissionList = new List<Permission>();
            Permission Temp = null;
            permissionList = GetAllPermission();
            for (int i = 0; i < permissionList.Count; i++)
            {
                if (permissionList[i].PermissionId == permissionId)
                {
                    Temp = permissionList[i];
                    break;
                }
            }
            return Temp;
        }

        /// <summary>
        /// 返回默认权限值列表
        /// </summary>
        /// <param name="permissionItemlst"></param>
        public static void GetDefaultPermissionItem(List<Sys_PermissionItem> permissionItemlst)
        {
            permissionItemlst.Add(new Sys_PermissionItem("访问", 1024, "index", false));
            permissionItemlst.Add(new Sys_PermissionItem("查看列表", 2, "list", false));
            permissionItemlst.Add(new Sys_PermissionItem("查看详情", 4, "look", false));
            permissionItemlst.Add(new Sys_PermissionItem("新增", 8, "insert", false));
            permissionItemlst.Add(new Sys_PermissionItem("修改", 16, "update", false));
            permissionItemlst.Add(new Sys_PermissionItem("删除", 32, "delete", false));
            permissionItemlst.Add(new Sys_PermissionItem("排序", 64, "orderby", false));
            permissionItemlst.Add(new Sys_PermissionItem("打印", 128, "print", false));
            permissionItemlst.Add(new Sys_PermissionItem("搜索", 256, "search", false));
            permissionItemlst.Add(new Sys_PermissionItem("批量删除", 512, "deletelist", false));
          
        }

        /// <summary>
        /// 根据controller 获取当前权限
        /// </summary>
        /// <param name="controller"></param>
        /// <returns></returns>
        public static Permission GetPermission(string controller)
        {
            List<Permission> permissionList = GetAllPermission();
            Permission permission=new Permission();

            if (permissionList != null)
            {
                for (int i = 0; i < permissionList.Count; i++)
                {
                    if (permissionList[i].PagePath.ToLower().IndexOf(string.Concat("/", controller.ToLower(), "/")) > -1)
                    {
                        permission = permissionList[i];

                    }
                }
            }
            return permission;
        }

        /// <summary>
        /// 是否具有权限
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="action"></param>
        /// <returns></returns>
        public static bool IsAllowed(string controller, string action)
        {
            List<Permission> permissionList = GetAllPermission();
            string pageCode = string.Empty;
            int permissionValue = 0;
            if (permissionList != null)
            {
                for (int i = 0; i < permissionList.Count; i++)
                {
                    if (permissionList[i].PagePath.ToLower().IndexOf(string.Concat("/", controller.ToLower(), "/")) > -1)
                    {
                        pageCode = permissionList[i].PageCode;
                        foreach (Sys_PermissionItem item in permissionList[i].ItemList)
                        {
                            if (item.PermissonCode == action.ToLower())
                            {
                                permissionValue = item.PermissionValue;
                                break;
                            }
                        }
                        break;
                    }
                }
                if (string.IsNullOrEmpty(pageCode))
                {
                    return true;
                }
                if (permissionValue == 0)
                {
                    return true;
                }

                if (UserData.CheckPageCode(Utils.GetUserID, pageCode, permissionValue))
                {
                    Sys_EventEntity sysEvent = new Sys_EventEntity();
                    sysEvent.DataTable_Action = DataTableAction.Insert;
                    sysEvent.E_DateTime = DateTime.Now;
                    sysEvent.E_From = Utils.GetUrlReferrer();
                    sysEvent.E_IP = Utils.GetIP();
                    sysEvent.E_M_Action = action;
                    Permission permission = new Permission();
                    permission = PermissionManger.GetPermission(controller);
                    sysEvent.E_M_Name = permission.PageCodeName;
                    sysEvent.E_M_PageCode = permission.PageCode;
                    sysEvent.E_Type = 1;
                    sysEvent.E_UserID = Utils.GetUserID;
                    sysEvent.E_U_LoginName = UserData.GetUserData.U_LoginName;
                    BLLHelper bll = new BLLHelper();
                    bll.Sys_EventInsertUpdateDelete(sysEvent);
                    return true;
                }
                else
                {
                    return false;
                }


            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 返回所有权限列表
        /// </summary>
        /// <returns></returns>
        public static List<Permission> GetAllPermission()
        {
            List<Permission> permissionList = new List<Permission>();
            string key = "All-Permission";
            if (CMNCache.Instance()[key] != null)
            {
                permissionList = (List<Permission>)CMNCache.Instance()[key];
            }
            else
            {
                QueryParam qp = new QueryParam();
                int recordCount = 0;
                BLLHelper bll = new BLLHelper();
                List<Sys_ModuleEntity> sys_moduleList = bll.Sys_ModuleList(qp, out recordCount);
                qp.Orderfld = "";
                List<Sys_ModuleExtPermissionEntity> moduleExt = bll.Sys_ModuleExtPermissionList(qp, out recordCount);
                List<Sys_PermissionItem> PermissionItemlst = new List<Sys_PermissionItem>();
                GetDefaultPermissionItem(PermissionItemlst);
                foreach (Sys_ModuleEntity module in sys_moduleList)
                {
                    if (module.M_ParentID != 0)
                    {
                        Permission permission = new Permission();
                        permission.PermissionId = module.ModuleID;
                        permission.PageCode = module.M_PageCode;
                        permission.PageCodeName = module.M_CName;
                        permission.PagePath = module.M_Directory;
                        for (int i = 0; i < PermissionItemlst.Count; i++)
                        {
                            permission.ItemList.Add(PermissionItemlst[i]);
                        }
                        for (int i = 0; i < moduleExt.Count; i++)
                        {
                            if (moduleExt[i].ModuleID == module.ModuleID)
                            {
                                permission.ItemList.Add(new Sys_PermissionItem(moduleExt[i].PermissionName, moduleExt[i].PermissionValue, moduleExt[i].PermissionCode.ToLower(), false));
                            }
                        }
                        permissionList.Add(permission);
                    }
                }

                CMNCache.Instance().Insert(key, permissionList);

            }
            return permissionList;

        }
    }
}
